﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Explorer;
using SPVSX.Explorer.Extensions.Properties;
using Microsoft.VisualStudio.SharePoint.Explorer.Extensions;
using System.Diagnostics;
using SPVSX.Explorer.Extensions.CommandIds;

namespace SPVSX.Explorer.Extensions
{
    partial class ListNodeExtension
    {
        /// <summary>
        /// Initialise the events for a web part gallery
        /// </summary>
        /// <param name="nodeType">The node type</param>
        public void InitializeWebPartGallery(IExplorerNodeType nodeType)
        {
            nodeType.NodeChildrenRequested += WebPartGallery_NodeChildrenRequested;
            nodeType.NodeMenuItemsRequested += WebPartGallery_NodeMenuItemsRequested;
            nodeType.NodePropertiesRequested += WebPartGallery_NodePropertiesRequested;
        }

        void WebPartGallery_NodePropertiesRequested(object sender, ExplorerNodePropertiesRequestedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void WebPartGallery_NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
        {
            if (e.Node.Text == Resources.WebPartGalleryNode_Label)
            {
                e.MenuItems.Add(Resources.WebPartGalleryNode_ViewInBrowser).Click += WebPartGalleryListNodeExtension_Click;
            }
        }

        void WebPartGalleryListNodeExtension_Click(object sender, MenuItemEventArgs e)
        {
            IExplorerNode owner = (IExplorerNode)e.Owner;

            string allItemsUrl =
            owner.Context.SharePointConnection.ExecuteCommand<string>(WebPartGalleryCommandIds.GetWebPartGalleryAllItemsUrl);

            if (allItemsUrl.StartsWith(@"/"))
            {
                allItemsUrl = allItemsUrl.TrimStart(@"/".ToCharArray());
            }
            Process.Start(new Uri(owner.Context.SiteUrl + allItemsUrl).AbsoluteUri);
        }

        /// <summary>
        /// Get the webpart nodes if the site is the root site
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The ExplorerNodeEventArgs object</param>
        void WebPartGallery_NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
            // The CreateCustomActionNodes argument is a delegate that Visual Studio calls 
            // to create the child nodes under the Web Part Gallery node.
            IListNodeInfo annotation = e.Node.GetAnnotation<IListNodeInfo>();
            if ((annotation != null) && (annotation.Title == Resources.WebPartGalleryNode_Label))
            {
                //Only render the node if the site is the root
                GenericFolderNode.Create(e.Node,
                    Resources.WebPartsNode_Label,
                    Resources.WebPartsNode.ToBitmap(),
                    WebPartGalleryNodeTypeProvider.GetWebParts);
            }
        }
    }
}
